const ffmpeg = require('fluent-ffmpeg')
const ffmpegPath = require('@ffmpeg-installer/ffmpeg')
const ffprobePath = require('@ffprobe-installer/ffprobe')

// 定义视频文件路径
ffmpeg.setFfmpegPath(ffmpegPath.path)
ffmpeg.setFfprobePath(ffprobePath.path)

/**
 * 获取视频时长
 * @param {*} path 
 * @returns 
 */
function getVideoDuration (dir) {
  return new Promise((resolve, reject) => {
    ffmpeg.ffprobe(dir, (error, data) => {
      if (error) {
        reject(error)
      } else {
        resolve(data.format.duration)
      }
    })
  })
}

/**
 * 获取文件后缀
 * @param {*} name 
 * @returns 
 */
function getSuffix (name) {
  const index = name.lastIndexOf('.')
  return name.slice(index + 1)
}


module.exports = {
  getVideoDuration,
  getSuffix
}